package cn.com.project.pattern.c.singleton;

/**
 * @Desc 单例模式
 */

/*
* 饿汉模式
* */
public class Singleton {
	// 私有化构造器，不让外界实例化对象
	private Singleton() {}
	private static final Singleton singleton = new Singleton();
	public static Singleton getInstance() {
		return singleton;
	}
}

/*
// 懒汉模式：
public class Singleton {
	private Singleton() {}
	private volatile static Singleton singleton = null;
	public static Singleton getInstance() {
		if (singleton == null) {
			synchronized (Singleton.class) {
				if (singleton == null) {
					singleton = new Singleton();
				}
			}
		}
		return singleton;
	}
}

// 内部类模式：
public class Singleton {
	private Singleton(){}
	private static class SingletonClass {
		private static final Singleton singleton = new Singleton();
	}
	public static Singleton getInstance() {
		return SingletonClass.singleton;
	}
}
*/
